package com.yanqu.road.web.controller.game;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.entity.forbiddenword.ForbiddenWordOverSeasData;
import com.yanqu.road.entity.web.HttpJsonWebParam;
import com.yanqu.road.server.helper.SignHelper;
import com.yanqu.road.server.manger.AppKeyMgr;
import com.yanqu.road.server.manger.ForbiddenWordOverSeasMgr;
import com.yanqu.road.utils.string.StringUtils;
import com.yanqu.road.web.controller.BaseJsonHttpServlet;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

@WebServlet(urlPatterns = "/queryForbiddenWordOverSeas")
public class QueryForbiddenWordOverSeas extends BaseJsonHttpServlet {

	private static Logger logger = LogManager.getLogger(QueryForbiddenWordOverSeas.class.getName());

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info("request queryForbiddenOverSeas");
		HttpJsonWebParam httpJsonWebParam = super.parseRequest(request);
		JSONObject paramObj = httpJsonWebParam.getParamObj();
		JSONObject resultObj = takePostResult(paramObj);
		sendMessage(response, resultObj.toJSONString());
	}

	private JSONObject takePostResult(JSONObject paramObj) {
		JSONObject resultObj = new JSONObject();
		resultObj.put("state", 1);
		resultObj.put("data", new ArrayList<>());
		resultObj.put("msg", "success");

		if (paramObj == null) {
			resultObj.put("state", 0);
			resultObj.put("msg", "request is null");
			return resultObj;
		}

		SortedMap<String, String> paramMap = new TreeMap<>();

		int gid = paramObj.getIntValue("gid");
		long time = paramObj.getLongValue("time");
		String sign = paramObj.getString("sign");
		if (sign == null) {
			resultObj.put("state", 0);
			resultObj.put("msg", "sign is null");
			return resultObj;
		}

		paramMap.put("gid", String.valueOf(gid));
		paramMap.put("time", String.valueOf(time));

//		String checkSign = SignHelper.getSign(paramMap, AppKeyMgr.getAndroidAppSecret());
//		if (!sign.equals(checkSign)) {
//			resultObj.put("state", 0);
//			resultObj.put("msg", "sign error");
//			return resultObj;
//		}

		List<ForbiddenWordOverSeasData> wordDataList = ForbiddenWordOverSeasMgr.getWordDataList();
		List<String> wordList = new ArrayList<>();
		for (ForbiddenWordOverSeasData forbiddenWordOverSeasData : wordDataList) {
			wordList.addAll(forbiddenWordOverSeasData.getWordSet());
		}
		resultObj.put("data", wordList);
		return resultObj;
	}
}
